<template>
  <div>
    <div style="text-align:right;margin-bottom: 10px">
      <el-tooltip class="item" effect="dark" content="新增业务员" placement="top">
        <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd" />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="刷新" placement="top">
        <el-button icon="el-icon-refresh" size="small" @click="handleGetList" />
      </el-tooltip>
    </div>
    <e-table
      ref="eTable"
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
    >
      <el-table-column slot="info" show-overflow-tooltip label="供应商" align="center">
        <span slot-scope="scope">
          <detail-button class="fl" :record="scope.row">{{ scope.row.supplierName }}</detail-button>
        </span>
      </el-table-column>
      <el-table-column slot="idcardState" label="身份证状态" align="center" width="110px">
        <span slot-scope="scope">
          <template v-if="scope.row.idcardFront">
            <!-- <el-button
              type="text"
              @click="handlePreview(scope.row, 'idcard')"
            >{{ `${scope.row.idcardEnd}` }}</el-button>-->
            {{ scope.row.idcardEnd }}
          </template>
          <template v-else>
            <span>未上传</span>
          </template>
        </span>
      </el-table-column>
      <el-table-column slot="qualification" label="购销资格证" width="110" align="center">
        <span slot-scope="scope">
          <template v-if="scope.row.qualification">
            <!-- <el-button
              type="text"
              @click="handlePreview(scope.row, 'qualification')"
            >{{ `${scope.row.qualificationEnd}` }}</el-button>-->
            {{ scope.row.qualificationEnd }}
          </template>
          <template v-else>
            <span>未上传</span>
          </template>
        </span>
      </el-table-column>
      <!-- <el-table-column slot="salesmanFlag" label="业务员" align="center" width="80">
        <span slot-scope="scope">
          <span v-if="scope.row.salesmanFlag">是</span>
          <span v-else>否</span>
        </span>
      </el-table-column>-->
      <el-table-column slot="action" fixed="right" label="操作" width="50" align="center">
        <template slot-scope="scope">
          <el-dropdown>
            <a class="el-dropdown-link">
              <i class="el-icon-menu"></i>
            </a>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleEdit(true, scope.row)">
                <a>编辑</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleEdit(true, scope.row, true)">
                <a>查看</a>
              </el-dropdown-item>
              <el-dropdown-item @click.native="handleDelete(scope.row)">
                <a class="red">删除</a>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </e-table>
    <preview-modal ref="previewModal" />
  </div>
</template>

<script>
import PreviewModal from '@/components/PreviewModal'
import DetailButton from '@/components/DetailButton'
import { linkmanList, deleteLinkMan, getOneUrl } from '@/api/supplier'
import { openNotification } from '@/utils/util'

export default {
  components: {
    PreviewModal,
    DetailButton
  },
  data() {
    return {
      queryParam: {
        pageNo: 1,
        pageSize: 10
      },
      loading: false,
      columns: [
        { slot: 'info' },
        {
          label: '姓名',
          align: 'center',
          width: '80px',
          prop: 'linkmanName'
        },
        {
          label: '头衔',
          align: 'center',
          width: '120px',
          prop: 'linkmanTitle'
        },
        {
          label: '联系方式',
          align: 'center',
          width: '120px',
          prop: 'linkmanTel'
        },
        {
          label: '身份证号',
          align: 'center',
          width: '190px',
          prop: 'idcard'
        },
        { slot: 'idcardState' },
        // { slot: 'salesmanFlag' },
        { slot: 'qualification' },
        { slot: 'action' }
      ],
      tableData: [],
      total: 1
    }
  },
  mounted() {
    this.handleGetList()
  },
  methods: {
    handleAdd() {
      this.$nextTick(() => {
        this.$emit('edit', false, {})
      })
    },
    // 预览图片处理
    handlePreview(record, type) {
      let id = ''
      if (type === 'idcard') {
        id = record.idcardFront
      } else {
        id = record.qualification
      }
      getOneUrl(id).then(res => {
        console.log('res', res)
        this.$refs.previewModal.show([res.data.id])
      })
    },
    handleEdit(bool, row, view = false) {
      console.log('edit', view)

      this.$nextTick(() => {
        this.$emit('edit', bool, row, view)
      })
    },
    handleDelete(row) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${row.linkmanName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          const res = await deleteLinkMan(row.linkmanId, {})
          openNotification('success', '删除成功')
          _this.handleGetList()
        }
      })
    },
    async handleGetList(param = {}) {
      this.loading = true
      const res = await linkmanList({ ...this.queryParam, ...param })
      if (res && res.data.records.length > 0) {
        const {
          state,
          data: { records, total }
        } = res
        this.tableData = records
        this.total = total
      } else {
        this.tableData = []
        this.total = 1
      }
      this.loading = false
    }
  }
}
</script>

<style scoped>
.tableExpandRow .ant-col-18 div {
  margin-bottom: 10px;
}

.contact-table >>> .ant-btn-link {
  padding: 0;
}
</style>
